package com.haizhi.crm.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.haizhi.crm.common.constant.SearchStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.search.constant.AtlasType;
import com.haizhi.crm.search.manager.GdbSearchManager;
import com.haizhi.crm.search.model.GdbSearchQo;
import com.haizhi.crm.search.model.GdbSearchResp;
import com.haizhi.crm.search.service.GdbSearchService;
import com.haizhi.crm.search.service.GdbTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by chengmo on 2018/6/14.
 */
@Service
public class GdbSearchServiceImpl implements GdbSearchService {
    private static final String GRAPH_FIELD = "graph";
    private static final String START_VERTICES_FIELD = "startVertices";

    @Autowired
    private GdbSearchManager gdbSearchManager;
    @Autowired
    private GdbTemplateService gdbTemplateService;

    @Override
    public GdbSearchResp standardAtlasExpand(GdbSearchQo searchQo) {
        String graph = searchQo.getGraph();
        String vertexId = searchQo.getVertexId();
        AtlasType atlasType = searchQo.getAtlasType();
        if (StringUtils.isAnyBlank(graph, vertexId) || atlasType == null) {
            throw new UnexpectedStatusException(SearchStatus.INVALID_PARAM_ERROR,
                    JSON.toJSONString(searchQo));
        }
        // template
        Map<String, Object> api = gdbTemplateService.getAtlasTemplate(searchQo.getAtlasType());
        api.put(GRAPH_FIELD, searchQo.getGraph());
        api.put(START_VERTICES_FIELD, Lists.newArrayList(searchQo.getVertexId()));
        return gdbSearchManager.standardAtlasExpand(api);
    }
}
